
<template>
    <el-scrollbar>
      <div>
        <div style="margin-bottom: 5px; margin-top: 5px;">
  
        <el-button type="success" @click="add" style= "margin-left: 470px; width: 300px; height: 40px;" >新增订单</el-button>
      </div>
            <el-table :data="tableData" 
            :header-cell-style="{background: '#B0C4DE', color:'#555','text-align': 'center'}"
                :cell-style="{padding:20+'px','text-align': 'center'}" 
                :border-color="black"
                style="margin-top:15px; font-size: 15px">
                <el-table-column prop="id" label="ID" width="100" />
                <el-table-column prop="username" label="用户名" width="130" />
              <el-table-column prop="operater" label="操作人" width="130"  />
              <el-table-column prop="style" label="类别" width="100" :formatter="formatStyle"  />
              <el-table-column prop="count" label="数量" width="60"  />
              <el-table-column prop="createtime" label="下单时间" width="220"  />
              <el-table-column prop="note" label="备注" width="150"  />

              <el-table-column prop="state" label="完成状态" width="120">
      <template #default="scope">
        <el-switch 
        v-model="scope.row.state" 
        :active-value="'是'"
        :inactive-value="'否'"
        active-text="是" 
        inactive-text="否"></el-switch>
      </template>
    </el-table-column>
              <el-table-column prop="operate" label="操作" >
                <template #default="scope">
                <el-button style="margin-left: 5px" type="success" @click="mod(scope.row)">编辑</el-button>
  
                <el-popconfirm title="确定要删除吗?" @confirm="del(scope.row.id)">
      <template #reference>
        <el-button style="margin-left: 5px" type="danger" >删除</el-button>

      </template>
    </el-popconfirm>
    <el-button v-if="scope.row.state === '是'" style="margin-left: 5px" type="success" @click="comment(scope.row)">评价</el-button>
              </template>
                </el-table-column>
            </el-table>
         
        <el-dialog v-model="centerDialogVisible" title="提示" width="30%" center>
      <span>
        <el-form :rules="rules" :model="form" ref="form" label-width="120px">
            <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="类别" prop="style">
        
        <el-select v-model="form.style" class="m-2" placeholder="请选择类别" size="large">
    <el-option
      v-for="item in cpData"
      :key="item.id"
      :label="item.style"
      :value="item.id"
    />
  </el-select>
      </el-form-item>
      <el-form-item label="数量" prop="count">
        <el-input v-model="form.count" />
      </el-form-item>
      <el-form-item label="备注" prop="note">
        <el-input v-model="form.note" />
      </el-form-item>
      </el-form>
      </span>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="centerDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="save">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>


    <el-dialog v-model="commentDialogVisible" title="提示" width="30%" center>
      <span>
        <el-form :rules="rules1" :model="form1" ref="form1" label-width="120px">
            <el-form-item label="评价" prop="content">
        <el-input v-model="form1.content" />
      </el-form-item>
      </el-form>
      </span>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="commentDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="save1">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>


          </div>
          </el-scrollbar>
  </template>
  
  <script>
  import axios from 'axios';
  import { Search } from '@element-plus/icons-vue'
  export default {
    data() {     
      return {
        cpData:[],
        tableData:[],
        SearchIcon:Search,
        centerDialogVisible:false,
        commentDialogVisible:false,
        user : JSON.parse(sessionStorage.getItem('CurUser')),
        operater:'',
        form:{
          style:'',
          count:'',
          note:'',
        },
        form1:{
          content:'',
          username: '',
          style:''
        },
        rules: {
            username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'}
          ],
          style: [
                        {required: true, message: '请输入类别', trigger: 'blur'}
          ],
          count: [
                        {required: true, message: '请输入数量', trigger: 'blur'}
                      ]
        },
        rules1: {
          content: [
          {required: true, message: '请输入评价', trigger: 'blur'}
          ]
        }
      }
    },
    created(){  
      this.getData();
      this.loadCP();
    },  
    methods:{
      save1(){
        axios.post('/comment/save',this.form1).then(response=> { 
          console.log(response.data)
          if(response.data.code == 200){
           this.$message({
                message:'新增成功！',
                type: 'success'
           });
           this.commentDialogVisible = false;
           this.getData();
          }else{
            this.$message({
                message:'新增失败，请重试！',
                type: 'error'
           });
          }
        })
      },
        formatStyle(row){
            let temp = this.cpData.find(item=>{
                return item.id == row.style
            })
            return temp && temp.style
        },
        loadCP(){
            axios.get('/cp/list').then(res=>res.data).then(res=>{
                    console.log(res)
                    if(res.code==200){
                        this.cpData=res.data
                    }else{
                        alert('获取数据失败')
                    }

                })
        },
      getData() {
        axios.get('/orderlist/userlist?username='+this.user.username).then(response=> { 
          console.log(response.data)
          if(response.data.code == 200){
            this.tableData = response.data.data;
            this.total = response.data.total
          }else{
            alert('获取数据失败')
          }
        }).catch(function (error) {
          console.log(error);
        });
      },
      resetParam(){
        this.username=''
      },
      add(){
        this.centerDialogVisible = true;
        this.$nextTick(()=>{
          this.resetForm();
          this.form.id = ''
        })
      },
     
      doSave(){
        axios.post('/orderlist/save',this.form).then(response=> { 
          console.log(response.data)
          if(response.data.code == 200){
           this.$message({
                message:'新增成功！',
                type: 'success'
           });
           this.centerDialogVisible = false;
           this.getData();
          }else{
            this.$message({
                message:'新增失败，请重试！',
                type: 'error'
           });
          }
        })
      },
      doMod(){
        axios.post('/orderlist/update',this.form).then(response=> { 
          console.log(response.data)
          if(response.data.code == 200){
           this.$message({
                message:'修改成功！',
                type: 'success'
           });
           this.centerDialogVisible = false;
           this.getData();
          }else{
            this.$message({
                message:'修改失败，请重试！',
                type: 'error'
           });
          }
        })
      },
      save(){
        this.$refs.form.validate((valid) => {
                      if (valid) {
                        if(this.form.id) {
                          this.doMod();
                        }else{
                          this.doSave();
                        }
                      } else {
                          console.log('error submit!!');
                          return false;
                      }
                  });   
      },
      resetForm(){
        this.$refs.form.resetFields();
      },
      mod(row){
        console.log(row);
        this.centerDialogVisible = true;
        this.form = row;
      },
      comment(row){
        console.log(row);
        this.commentDialogVisible = true;
        this.form1 = row;
      },
      del(id){
        //console.log(id);
        axios.get('/orderlist/del?id='+id).then(response=> { 
          console.log(response.data)
          if(response.data.code == 200){
           this.$message({
                message:'删除成功！',
                type: 'success'
           });
           this.getData();
          }else{
            this.$message({
                message:'删除失败，请重试！',
                type: 'error'
           });
          }
        })
      }
    }
  }
    
  </script>